package com.jingdianjichi.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.auth.application.convert.AuthRoleDTOConverter;
import com.jingdianjichi.auth.application.dto.AuthRoleDTO;
import com.jingdianjichi.auth.entity.Result;
import com.jingdianjichi.auth.domain.entity.AuthRoleBO;
import com.jingdianjichi.auth.domain.service.AuthRoleDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 角色DTO
 */
@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {
    @Resource
    private AuthRoleDomainService authRoleDomainService;

    /**
     * 新增角色
     */
    @RequestMapping("/add")
    public Result<Boolean> add(@RequestBody AuthRoleDTO authRoleDTO){
        try {
            if (log.isInfoEnabled()){
                log.info("RoleController.add.dto:{}", JSON.toJSONString(authRoleDTO));
            }

            //参数校验
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleKey()),"角色key不能是空的");
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleName()),"角色名称不是空的");

            AuthRoleBO authRoleBO= AuthRoleDTOConverter.INSTANCE.convertDTOToBO(authRoleDTO);

            Boolean result=authRoleDomainService.add(authRoleBO);

            return Result.ok(result);

        }catch (Exception e){
            log.error("RoleController.add.error:{}",e.getMessage(),e);

            return Result.fail("角色新增失败");
        }
    }

    /**
     * 修改角色信息
     */
    @RequestMapping("/update")
    public Result<Boolean> update(@RequestBody AuthRoleDTO authRoleDTO){
        try {
            if (log.isInfoEnabled()){
                log.info("RoleController.update.dto:{}", JSON.toJSONString(authRoleDTO));
            }

            //参数校验
            Preconditions.checkNotNull(authRoleDTO.getId(),"角色id不能是空的");

            AuthRoleBO authRoleBO= AuthRoleDTOConverter.INSTANCE.convertDTOToBO(authRoleDTO);

            Boolean result=authRoleDomainService.update(authRoleBO);

            return Result.ok(result);

        }catch (Exception e){
            log.error("RoleController.update.error:{}",e.getMessage(),e);

            return Result.fail("角色修改失败");
        }
    }

    /**
     * 删除角色信息
     */
    @RequestMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthRoleDTO authRoleDTO){
        try {
            if (log.isInfoEnabled()){
                log.info("RoleController.delete.dto:{}", JSON.toJSONString(authRoleDTO));
            }

            AuthRoleBO authRoleBO= AuthRoleDTOConverter.INSTANCE.convertDTOToBO(authRoleDTO);

            Boolean result=authRoleDomainService.delete(authRoleBO);

            return Result.ok(result);

        }catch (Exception e){
            log.error("RoleController.delete.error:{}",e.getMessage(),e);

            return Result.fail("角色删除失败");
        }
    }

}
